package com.yfwl.ex2uiex1;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ValidationActivity extends Activity {

    // 当前实时输入的密码
    private String stringPassword = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_validation);


        int[] pwBtnIdArray = new int[]{R.id.ib_pw_btn1, R.id.ib_pw_btn2, R.id.ib_pw_btn3, R.id.ib_pw_btn4, R.id.ib_pw_btn5, R.id.ib_pw_btn6, R.id.ib_pw_btn7,
                R.id.ib_pw_btn8, R.id.ib_pw_btn9, R.id.ib_pw_btn10, R.id.ib_pw_btn11, R.id.ib_pw_btn12 };
        for (int i = 0; i < 12; i++ )
        {
            setPwBtnsListener(i, pwBtnIdArray);
        }



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.validation, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void updateTheUI ()
    {
        ImageView iv_icon_pw1 = (ImageView) findViewById(R.id.iv_icon_pw1);
        ImageView iv_icon_pw2 = (ImageView) findViewById(R.id.iv_icon_pw2);
        ImageView iv_icon_pw3 = (ImageView) findViewById(R.id.iv_icon_pw3);
        ImageView iv_icon_pw4 = (ImageView) findViewById(R.id.iv_icon_pw4);
        if (stringPassword.length() == 0)
        {
            iv_icon_pw1.setImageResource(R.drawable.icon_pw);
            iv_icon_pw2.setImageResource(R.drawable.icon_pw);
            iv_icon_pw3.setImageResource(R.drawable.icon_pw);
            iv_icon_pw4.setImageResource(R.drawable.icon_pw);
        }
        if (stringPassword.length() == 1)
        {
            iv_icon_pw1.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw2.setImageResource(R.drawable.icon_pw);
            iv_icon_pw3.setImageResource(R.drawable.icon_pw);
            iv_icon_pw4.setImageResource(R.drawable.icon_pw);
        }
        if (stringPassword.length() == 2)
        {
            iv_icon_pw1.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw2.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw3.setImageResource(R.drawable.icon_pw);
            iv_icon_pw4.setImageResource(R.drawable.icon_pw);
        }
        if (stringPassword.length() == 3)
        {
            iv_icon_pw1.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw2.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw3.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw4.setImageResource(R.drawable.icon_pw);
        }
        if (stringPassword.length() == 4)
        {
            iv_icon_pw1.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw2.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw3.setImageResource(R.drawable.icon_pw2);
            iv_icon_pw4.setImageResource(R.drawable.icon_pw2);
        }

    }

    private void setPwBtnsListener(int position, int[] pwBtnIdArray)
    {
        if (position >= 0 && position <= 8)
        {
            final int pwBtnNumber = position + 1;
            ImageButton imageButtonDigits =  (ImageButton) findViewById(pwBtnIdArray[position]);
            imageButtonDigits.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (stringPassword.length() >= 0 &&stringPassword.length() < 4)
                    {
                        stringPassword = stringPassword + pwBtnNumber;
                        updateTheUI();
                        if (stringPassword.length() == 4)
                        {
                            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if ("4927".equals(stringPassword))
                                    {
                                        Intent intent = new Intent(ValidationActivity.this, HomeActivity.class);
                                        startActivity(intent);
                                        ValidationActivity.this.finish();
                                    }
                                    else
                                    {
                                        TextView tv_validation_tips = (TextView) findViewById(R.id.tv_validation_tips);
                                        tv_validation_tips.setVisibility(TextView.VISIBLE);

                                        stringPassword = "";
                                        updateTheUI();
                                    }
                                }
                            },500);
                        }

                    }

                }
            });
        }

        if (position == 9)
        {
            ImageButton imageButtonDigits =  (ImageButton) findViewById(pwBtnIdArray[position]);
            imageButtonDigits.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ValidationActivity.this.finish();
                }
            });
        }
        if (position == 10)
        {
            final int pwBtnNumber = 0;
            ImageButton imageButtonDigits =  (ImageButton) findViewById(pwBtnIdArray[position]);
            imageButtonDigits.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (stringPassword.length() >= 0 &&stringPassword.length() < 4)
                    {
                        stringPassword = stringPassword + pwBtnNumber;
                        updateTheUI();
                        if (stringPassword.length() == 4)
                        {
                            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if ("4927".equals(stringPassword))
                                    {
                                        Intent intent = new Intent(ValidationActivity.this, HomeActivity.class);
                                        startActivity(intent);
                                        ValidationActivity.this.finish();
                                    }
                                    else
                                    {
                                        TextView tv_validation_tips = (TextView) findViewById(R.id.tv_validation_tips);
                                        tv_validation_tips.setVisibility(TextView.VISIBLE);

                                        stringPassword = "";
                                        updateTheUI();
                                    }
                                }
                            },500);
                        }



                    }

                }
            });
        }
        if (position == 11)
        {
            ImageButton imageButtonDigits =  (ImageButton) findViewById(pwBtnIdArray[position]);
            imageButtonDigits.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (stringPassword.length() > 0 &&stringPassword.length() < 4)
                    {
                        stringPassword = stringPassword.substring(0, stringPassword.length() - 1);
                        updateTheUI();
                    }
                }
            });

        }

    }
}
